package com.fc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fc.checkGroup.Delete;
import com.fc.checkGroup.Insert;
import com.fc.checkGroup.Update;
import com.fc.entity.Category;
import com.fc.entity.Product;
import com.fc.entity.input.DeleteBatchInput;
import com.fc.entity.input.ProductInput;
import com.fc.entity.input.ProductQueryInput;
import com.fc.entity.output.CategoryOutput;
import com.fc.entity.output.ProductOutput;
import com.fc.entity.query.PageQuery;
import com.fc.entity.query.PageResult;
import com.fc.service.ProductService;
import com.fc.utils.ResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 产品表 前端控制器
 * </p>
 *
 * @author
 * @since 2024-11-05
 */
@RestController
@RequestMapping("/product")
@Tag(name = "产品接口")
@RequiredArgsConstructor
@Slf4j
@Validated
public class ProductController {

    private final ProductService productService;

    /**
     * 新增
     */
    @Operation(summary = "新增")
    @PostMapping("/add")
    public ResultVO<String> add(@Validated(Insert.class) @RequestBody ProductInput input) {
        log.info("产品新增");
        return productService.add(input) ? ResultVO.success("新增成功") : ResultVO.error("新增失败");
    }

    /**
     * 分页模糊查询
     */
    @Operation(summary = "分页查询")
    @GetMapping("page")
    public ResultVO<PageResult<ProductOutput>> getPage(PageQuery query, ProductQueryInput input) {
        log.info("产品分页查询");
        IPage<Product> page = productService.getPage(query, input);
        return ResultVO.success(PageResult.convertBuilder().dataSrc(page).to(ProductOutput.class).build());
    }

    /**
     * 修改
     */
    @Operation(summary = "修改")
    @PostMapping("/renew")
    public ResultVO<String> renew(@Validated(Update.class) @RequestBody ProductInput input) {
        log.info("产品修改");
        return productService.renew(input) ? ResultVO.success("修改成功") : ResultVO.error("修改失败");
    }

    /**
     * 删除
     */
    @Operation(summary = "分类删除")
    @PostMapping("del")
    public ResultVO<String> del(@Validated(Delete.class) @RequestBody DeleteBatchInput input) {
        log.info("产品删除");
        return productService.del(input) ? ResultVO.success("删除成功") : ResultVO.error("删除失败");
    }


    /**
     * 查询详情
     */
    @Operation(summary = "查看详情")
    @GetMapping("detail")
    public ResultVO<ProductOutput> detail(@NotNull Long id) {
        log.info("产品查看详情");
        return productService.detail(id);
    }
}
